package com.saibo.spider.gather;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.client.methods.CloseableHttpResponse;

import com.saibo.spider.commons.SubmitRedisCommons;
import com.saibo.spider.pojo.LetvVplayVideo;
import com.saibo.spider.utils.HttpRequestUtil;
import com.saibo.spider.utils.HttpResponseUtil;
import com.saibo.spider.utils.MD5Util;
import com.saibo.spider.utils.StringUtil;

/**
 * 获取乐视中某电视剧的剧集列表 http://www.le.com/ptv/vplay/27424673.html
 * @author TerransForce
 * 
 */
public class MouldLetvGetEpisode {

	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
		MouldLetvGetEpisode a = new MouldLetvGetEpisode();
		String url = "http://d.api.m.le.com/apipccard/dynamic?id=10020129&cid=2&vid=27424673&platform=pc&isvip=0&type=episode%2Cotherlist%2Crelalbum";
		CloseableHttpResponse httpResponse = HttpRequestUtil.getRequestMethod(url,a.requestHeader("http://www.le.com/ptv/vplay/27424673.html"));
		String json = HttpResponseUtil.getHtml(httpResponse);
		Map<String, Object> jsonMap = StringUtil.jsonToMap(json);
		String code = jsonMap.get("code").toString();
		String msg = jsonMap.get("msg").toString();
		System.out.println(code + "\t" + msg);
		Map<String,Object> dataMap = (Map<String, Object>) jsonMap.get("data");
		Map<String,Object> episodeMap = (Map<String, Object>) dataMap.get("episode");
		List<Map<String,Object>> videolist = (List<Map<String, Object>>) episodeMap.get("videolist");
		List<LetvVplayVideo> llvv = new ArrayList<LetvVplayVideo>();
		for (Map<String, Object> map : videolist) {
			LetvVplayVideo lvv =new LetvVplayVideo();
			System.out.println(StringUtil.mapToJson(map));
			String duration_Str = map.get("duration").toString();
			//System.out.println(duration_Str);
			String[] duSc = duration_Str.split(":");
			int min = Integer.parseInt(duSc[0]);
			int sec = Integer.parseInt(duSc[1]);
			int totalSecond = min*60+sec;
			String title = map.get("title").toString();
			String subtitle = map.get("subTitle").toString();
			String url_ = map.get("url").toString();
			//System.out.println(duration_Str+"\t"+totalSecond);
			String pic = map.get("pic").toString();
			String episode = map.get("episode").toString();
			String vid = map.get("vid").toString();
			String vidtype = map.get("videoType").toString();
			String releaseDate = map.get("releaseDate").toString();
			String pid = "11111";
			String name = "孤芳不自赏";
			lvv.setRowkey(MD5Util.GET16BITMD5(url_));
			lvv.setTitle(title);
			lvv.setSubtitle(subtitle);
			lvv.setEpisode(episode);
			DateFormat format= new SimpleDateFormat("yyyy-MM-dd"); 
			Date data = null;
			try {
				data = format.parse(releaseDate);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			lvv.setReleaseDate(data);
			lvv.setUrl(url_);
			lvv.setPic(pic);
			lvv.setVid(vid);
			lvv.setVideotype(vidtype);
			lvv.setDuration(totalSecond);
			lvv.setPid(pid);
			lvv.setName(name);
			llvv.add(lvv);
		}
		SubmitRedisCommons.submitRedisData("letv_episode_info", StringUtil.objectToJson(llvv));
	}

	public Map<String, String> requestHeader(String referer) {
		Map<String, String> requestHeader = new HashMap<String, String>();
		requestHeader.put("Accept", "*/*");
		requestHeader.put("Accept-Encoding", "gzip, deflate, sdch");
		requestHeader.put("Accept-Language", "zh-CN,zh;q=0.8");
		requestHeader.put("Connection", "keep-alive");
		requestHeader.put("Cookie",
				"vjuids=-3b4c0553b.1596da1d049.0.73c62c24b711b; ark_uuid=58196445727148d894f73efc84b20b83; tj_uuid=-_14842756424192403074; tj_lc=ec3ef18552206eadfdfe4d98f430bb7a; tj_env=1; bd_xid=ec3ef18552206eadfdfe4d98f430bb7a; newVideo=%7B%221%22%3A17%2C%222%22%3A123%2C%223%22%3A165%2C%224%22%3A699%2C%225%22%3A46%2C%228%22%3A0%2C%229%22%3A52%2C%2211%22%3A126%2C%2214%22%3A36%2C%2216%22%3A13%2C%2217%22%3A0%2C%2219%22%3A0%2C%2220%22%3A55%2C%2222%22%3A46%2C%2223%22%3A50%2C%2230%22%3A3192%2C%2232%22%3A0%2C%2233%22%3A0%2C%2234%22%3A0%2C%2235%22%3A0%2C%2236%22%3A9%2C%2238%22%3A0%2C%2239%22%3A0%2C%2240%22%3A0%2C%2242%22%3A0%2C%2243%22%3A0%2C%2245%22%3A10%2C%2246%22%3A0%2C%2247%22%3A0%2C%2248%22%3A0%2C%2249%22%3A0%2C%2250%22%3A0%2C%2251%22%3A0%2C%2252%22%3A0%2C%2253%22%3A0%2C%2254%22%3A0%2C%2255%22%3A0%2C%2260%22%3A0%2C%22200%22%3A0%2C%22333%22%3A0%2C%221009%22%3A1882%2C%221021%22%3A27%2C%221029%22%3A16%2C%221035%22%3A12%7D; sso_curr_country=CN; language=zh-cn; vjlast=1483603038.1484275675.13; currentLeft_miniPlayer=492; currentTop_miniPlayer=722; his_vid=27424673-27434584; tj_v2c=-27424673_2-27416375_2; unLogPlayTime=4");
		requestHeader.put("Host", "d.api.m.le.com");
		requestHeader.put("Referer", referer);
		requestHeader.put("User-Agent",
				"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36");
		return requestHeader;
	}
}
